<?php
    class Message extends MY_Controller {


        //初始化
        public function index()
        {
            $query="SELECT * FROM `crazy_message` order by createtime desc ";

            $list=$this->db->query($query);
            //从数据库中获取列表总数
            $num= $list->num_rows();

            //查询条件
            $search_str='';

            //分类数据初始化
            $params = array('total' => $num, 'listRows' => '15','pa'=>$search_str);

            //分页类初始化
            $this->load->library('Page',$params);

            //分页SQL语句
            $search_query= $query."  limit {$this->page->limit} ";

            //获取分页结果
            $reslut_list= $this->db->query($search_query);

            $data['list']=$reslut_list;

            $data['page']= $this->page->fpage();
            $this->load->view('admin/message_index',$data);
        }

        public function view()
        {
            $id = $this->input->get('id',true); 
            $this->load->helper('common_function');
            $this->load->model('admin/Message_model');
            $model=$this->Message_model->message_by_id_one($id);
            if($model)
            {
                
                //将留言状态改为已查看
                $object=array(
                    'state'=>1
                );
                $this->Message_model->get_message_modify($id,$object);
                
                $data['model']=$model;
                $this->load->view('admin/message_view',$data);

            }
            else
            {
                ShowMsg('留言不存在!',-1,0,1000);
                exit; 
            }
        }

        //删除
        public function del()
        {
            
            $this->load->helper('common_function');
            $aids = $this->input->get('aids',true); 
            $aids = explode(',',$aids);
            if(!empty($aids) && is_array($aids))
            {
                foreach($aids as $aid)
                {
                    $aid = preg_replace("#[^0-9]#", "", $aid);
                    $this->load->model('admin/Message_model');
                    $this->Message_model->get_message_del($aid);
                }
                ShowMsg('删除成功!', site_url("admin/message"),0,1000);
                exit; 
            }
            else
            {
                ShowMsg('删除失败!', site_url("admin/message"),0,1000);
                exit; 
            }
        }

    }
?>